package com.huxuesong.prcc.component;

import com.huxuesong.prcc.exception.ApiMallPlusException;
import com.huxuesong.prcc.util.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

//@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    private static final String logExceptionFormat = "Capture Exception By GlobalExceptionHandler: Code: %s Detail: %s";
    //运行时异常
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public CommonResult runtimeExceptionHandler(RuntimeException ex) {
        return new CommonResult().runtimeException(ex.getMessage());
    }

    //空指针异常
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public CommonResult nullPointerExceptionHandler(NullPointerException ex) {
        log.error(ex.toString());
        return new CommonResult().nullPointerException(ex.getMessage());
    }

    //类型转换异常
    @ExceptionHandler(ClassCastException.class)
    @ResponseBody
    public CommonResult classCastExceptionHandler(ClassCastException ex) {
        return new CommonResult().classCastException(ex.getMessage());
    }

    //IO异常
    @ExceptionHandler(IOException.class)
    @ResponseBody
    public CommonResult iOExceptionHandler(IOException ex) {
        return new CommonResult().ioException(ex.getMessage());
    }

    //未知方法异常
    @ExceptionHandler(NoSuchMethodException.class)
    @ResponseBody
    public CommonResult noSuchMethodExceptionHandler(NoSuchMethodException ex) {
        return new CommonResult().noSuchMethodException(ex.getMessage());
    }

    //数组越界异常
    @ExceptionHandler(IndexOutOfBoundsException.class)
    @ResponseBody
    public CommonResult indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex) {
        return new CommonResult().indexOutOfBoundsException(ex.getMessage());
    }

    //400错误
    @ExceptionHandler({HttpMessageNotReadableException.class})
    @ResponseBody
    public CommonResult requestNotReadable(HttpMessageNotReadableException ex) {
        System.out.println("400..requestNotReadable");
        return new CommonResult().httpMessageNotReadableException(ex.getMessage());
    }

    //400错误
    @ExceptionHandler({TypeMismatchException.class})
    @ResponseBody
    public CommonResult requestTypeMismatch(TypeMismatchException ex) {
        System.out.println("400..TypeMismatchException");
        return new CommonResult().typeMismatchException(ex.getMessage());
    }

    //400错误
    @ExceptionHandler({MissingServletRequestParameterException.class})
    @ResponseBody
    public CommonResult requestMissingServletRequest(MissingServletRequestParameterException ex) {
        System.out.println("400..MissingServletRequest");
        return new CommonResult().missingServletRequestParameterException(ex.getMessage());
    }

    //405错误
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    @ResponseBody
    public CommonResult request405(HttpRequestMethodNotSupportedException ex) {
        return new CommonResult().httpRequestMethodNotSupportedException(ex.getMessage());
    }

    //406错误
    @ExceptionHandler({HttpMediaTypeNotAcceptableException.class})
    @ResponseBody
    public CommonResult request406(HttpMediaTypeNotAcceptableException ex) {
        System.out.println("406...");
        return new CommonResult().httpMediaTypeNotAcceptableException(ex.getMessage());
    }

    //500错误
    @ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
    @ResponseBody
    public CommonResult server500(RuntimeException ex) {
        System.out.println("500...");
        return new CommonResult().runtimeException( ex.getMessage());
    }

    //栈溢出
    @ExceptionHandler({StackOverflowError.class})
    @ResponseBody
    public CommonResult requestStackOverflow(StackOverflowError ex) {
        return new CommonResult().stackOverflowError( ex.getMessage());
    }

    //其他错误
    @ExceptionHandler({Exception.class})
    @ResponseBody
    public CommonResult exception(Exception ex) {
        return new CommonResult().unKnown(ex.getMessage());
    }

    @ExceptionHandler({ApiMallPlusException.class, UsernameNotFoundException.class})
    @ResponseBody
    public CommonResult apiMallPlusException(Exception ex) {
        return new CommonResult().unauthorized(ex.getMessage());
    }

}
